package at.ac.tuwien.big.form.htmlgen;

import at.ac.tuwien.big.form.Form;
import at.ac.tuwien.big.form.Heading;
import at.ac.tuwien.big.form.InputField;
import at.ac.tuwien.big.form.List;
import at.ac.tuwien.big.form.ListItem;
import at.ac.tuwien.big.form.Page;
import at.ac.tuwien.big.form.PageElement;
import at.ac.tuwien.big.form.Paragraph;
import at.ac.tuwien.big.form.SelectionCondition;
import at.ac.tuwien.big.form.SelectionField;
import at.ac.tuwien.big.form.SelectionFieldType;
import at.ac.tuwien.big.form.SelectionItem;
import at.ac.tuwien.big.form.TextArea;
import at.ac.tuwien.big.form.TextField;
import at.ac.tuwien.big.form.VisibilityCondition;
import com.google.common.base.Objects;
import java.util.Arrays;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.IGenerator;

/**
 * open questions
 * is the resource set always in correct order (resp.. first welcome page.... a.s.o)
 */
@SuppressWarnings("all")
public class Form2HTMLGenerator implements IGenerator {
  private final String fileName = "registration.html";
  
  public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
    EList<EObject> _contents = resource.getContents();
    for (final EObject eobj : _contents) {
      if ((eobj instanceof Form)) {
        Form out = ((Form) eobj);
        CharSequence _generateHTML = this.generateHTML(out);
        fsa.generateFile(this.fileName, _generateHTML);
      }
    }
  }
  
  public CharSequence generateHTML(final Form form) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<!DOCTYPE html>");
    _builder.newLine();
    _builder.append("<html lang=\"en\">");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("<head>");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("<title>Registration information</title>");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"/>");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("<link href=\"../assets/form.css\" rel=\"stylesheet\" media=\"screen\"/>");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("<script src=\"../assets/jquery-1.10.2.min.js\" type=\"text/javascript\"></script>");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("<script src=\"../assets/form.js\" type=\"text/javascript\"></script>");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("<script type=\"text/javascript\">");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("$(document).ready(");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("function(){");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("form.init(); ");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    String _xblockexpression = null;
    {
      String out = "";
      EList<Page> _pages = form.getPages();
      for (final Page page : _pages) {
        {
          EList<PageElement> _elements = page.getElements();
          for (final PageElement element : _elements) {
            String _switchResult = null;
            boolean _matched = false;
            if (!_matched) {
              if (element instanceof InputField) {
                final InputField _inputField = (InputField)element;
                boolean _isMandatory = _inputField.isMandatory();
                if (_isMandatory) {
                  _matched=true;
                  StringConcatenation _builder_1 = new StringConcatenation();
                  _builder_1.append("form.registerElement(\'");
                  String _elementId = _inputField.getElementId();
                  _builder_1.append(_elementId, "");
                  _builder_1.append("\');");
                  _builder_1.newLineIfNotEmpty();
                  _builder_1.append("form.registerMandatory(\'");
                  String _elementId_1 = _inputField.getElementId();
                  _builder_1.append(_elementId_1, "");
                  _builder_1.append("\');");
                  _builder_1.newLineIfNotEmpty();
                  _switchResult = _builder_1.toString();
                }
              }
            }
            if (!_matched) {
              if (element instanceof InputField) {
                final InputField _inputField = (InputField)element;
                boolean _isMandatory = _inputField.isMandatory();
                boolean _not = (!_isMandatory);
                if (_not) {
                  _matched=true;
                  StringConcatenation _builder_1 = new StringConcatenation();
                  _builder_1.append("form.registerElement(\'");
                  String _elementId = _inputField.getElementId();
                  _builder_1.append(_elementId, "");
                  _builder_1.append("\');");
                  _builder_1.newLineIfNotEmpty();
                  _builder_1.append("form.registerOptional(\'");
                  String _elementId_1 = _inputField.getElementId();
                  _builder_1.append(_elementId_1, "");
                  _builder_1.append("\');");
                  _builder_1.newLineIfNotEmpty();
                  _switchResult = _builder_1.toString();
                }
              }
            }
            if (!_matched) {
              StringConcatenation _builder_1 = new StringConcatenation();
              _builder_1.append("form.registerElement(\'");
              String _elementId = element.getElementId();
              _builder_1.append(_elementId, "");
              _builder_1.append("\');");
              _builder_1.newLineIfNotEmpty();
              _switchResult = _builder_1.toString();
            }
            String _concat = out.concat(_switchResult);
            out = _concat;
          }
          EList<VisibilityCondition> _visibilityConditions = page.getVisibilityConditions();
          for (final VisibilityCondition condition : _visibilityConditions) {
            {
              final SelectionCondition cond = ((SelectionCondition) condition);
              StringConcatenation _builder_2 = new StringConcatenation();
              _builder_2.append("form.registerConditional(\'");
              SelectionItem _item = cond.getItem();
              SelectionField _field = _item.getField();
              String _elementId_1 = _field.getElementId();
              _builder_2.append(_elementId_1, "");
              _builder_2.append("\',\'");
              SelectionItem _item_1 = cond.getItem();
              String _label = _item_1.getLabel();
              _builder_2.append(_label, "");
              _builder_2.append("\',");
              {
                EList<PageElement> _concernsElements = cond.getConcernsElements();
                boolean _hasElements = false;
                for(final PageElement pe : _concernsElements) {
                  if (!_hasElements) {
                    _hasElements = true;
                    _builder_2.append("[\'", "");
                  } else {
                    _builder_2.appendImmediate("\',\'", "");
                  }
                  String _elementId_2 = pe.getElementId();
                  _builder_2.append(_elementId_2, "");
                }
                if (_hasElements) {
                  _builder_2.append("\']", "");
                }
              }
              _builder_2.append(");");
              _builder_2.newLineIfNotEmpty();
              String _concat_1 = out.concat(_builder_2.toString());
              out = _concat_1;
            }
          }
        }
      }
      _xblockexpression = (out);
    }
    _builder.append(_xblockexpression, "				");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t\t");
    _builder.append("});");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("</script>\t\t\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("</head>");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("<body>");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("<div class=\"container\">");
    _builder.newLine();
    {
      EList<Page> _pages = form.getPages();
      boolean _hasElements = false;
      for(final Page page : _pages) {
        if (!_hasElements) {
          _hasElements = true;
          _builder.append("\t\t\t<div class=\"page\">\n\t\t\t\t<div class=\"well\">\n\t\t\t\t\t<form action=\"#\">\n\t\t\t\t\t\t<fieldset>", "");
        } else {
          _builder.appendImmediate("\n\t\t\t\t\t\t</fieldset>\n\t\t\t\t\t</form>\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t\t<div class=\"page\">\n\t\t\t\t<div class=\"well\">\n\t\t\t\t\t<form action=\"#\">\n\t\t\t\t\t\t<fieldset>", "");
        }
        _builder.newLine();
        {
          EList<PageElement> _elements = page.getElements();
          for(final PageElement pe : _elements) {
            _builder.append("\t\t\t\t\t\t\t");
            CharSequence _extractPeInfo = this.extractPeInfo(pe);
            _builder.append(_extractPeInfo, "							");
            _builder.newLineIfNotEmpty();
          }
        }
      }
      if (_hasElements) {
        _builder.append("\n\t\t\t\t\t\t</fieldset>\n\t\t\t\t\t</form>\n\t\t\t\t</div>\n\t\t\t</div>", "");
      }
    }
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("</div>");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("</body>");
    _builder.newLine();
    _builder.append("</html>");
    _builder.newLine();
    return _builder;
  }
  
  protected CharSequence _extractPeInfo(final Heading heading) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<h");
    int _level = heading.getLevel();
    _builder.append(_level, "");
    _builder.append(" id=\"");
    String _elementId = heading.getElementId();
    _builder.append(_elementId, "");
    _builder.append("\">");
    String _content = heading.getContent();
    _builder.append(_content, "");
    _builder.append("</h");
    int _level_1 = heading.getLevel();
    _builder.append(_level_1, "");
    _builder.append(">");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  protected CharSequence _extractPeInfo(final Paragraph paragraph) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<p id=\"");
    String _elementId = paragraph.getElementId();
    _builder.append(_elementId, "");
    _builder.append("\">");
    String _content = paragraph.getContent();
    _builder.append(_content, "");
    _builder.append("</p>");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  protected CharSequence _extractPeInfo(final List list) {
    StringConcatenation _builder = new StringConcatenation();
    {
      boolean _isOrdered = list.isOrdered();
      if (_isOrdered) {
        _builder.append("<ol id=\"");
        String _elementId = list.getElementId();
        _builder.append(_elementId, "");
        _builder.append("\">");
        _builder.newLineIfNotEmpty();
        {
          EList<ListItem> _items = list.getItems();
          for(final ListItem bullet : _items) {
            _builder.append("\t");
            _builder.append("<li>");
            String _label = bullet.getLabel();
            _builder.append(_label, "	");
            _builder.append("</li>");
            _builder.newLineIfNotEmpty();
          }
        }
        _builder.append("</ol>");
        _builder.newLine();
      }
    }
    {
      boolean _isOrdered_1 = list.isOrdered();
      boolean _not = (!_isOrdered_1);
      if (_not) {
        _builder.append("<ul id=\"");
        String _elementId_1 = list.getElementId();
        _builder.append(_elementId_1, "");
        _builder.append("\">");
        _builder.newLineIfNotEmpty();
        {
          EList<ListItem> _items_1 = list.getItems();
          for(final ListItem bullet_1 : _items_1) {
            _builder.append("\t");
            _builder.append("<li>");
            String _label_1 = bullet_1.getLabel();
            _builder.append(_label_1, "	");
            _builder.append("</li>");
            _builder.newLineIfNotEmpty();
          }
        }
        _builder.append("</ul>");
        _builder.newLine();
      }
    }
    return _builder;
  }
  
  protected CharSequence _extractPeInfo(final TextField tfield) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<div class=\"control-group\">");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("<label for=\"");
    String _elementId = tfield.getElementId();
    _builder.append(_elementId, "	");
    _builder.append("\">");
    String _label = tfield.getLabel();
    _builder.append(_label, "	");
    _builder.append("</label>");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("<div class=\"controls\">");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("<input type=\"text\" id=\"");
    String _elementId_1 = tfield.getElementId();
    _builder.append(_elementId_1, "		");
    _builder.append("\"/>");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("</div>");
    _builder.newLine();
    _builder.append("</div>");
    return _builder;
  }
  
  protected CharSequence _extractPeInfo(final TextArea tarea) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<div class=\"control-group\">");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("<label for=\"");
    String _elementId = tarea.getElementId();
    _builder.append(_elementId, "	");
    _builder.append("\">");
    String _label = tarea.getLabel();
    _builder.append(_label, "	");
    _builder.append("</label>");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("<div class=\"controls\">");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("<textarea id=\"");
    String _elementId_1 = tarea.getElementId();
    _builder.append(_elementId_1, "		");
    _builder.append("\"></textarea>");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("</div>");
    _builder.newLine();
    _builder.append("</div>");
    return _builder;
  }
  
  protected CharSequence _extractPeInfo(final SelectionField selfield) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<div class=\"control-group\">");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("<label class=\"lone\" id=\"label_for_");
    String _elementId = selfield.getElementId();
    _builder.append(_elementId, "	");
    _builder.append("\">");
    String _label = selfield.getLabel();
    _builder.append(_label, "	");
    _builder.append("</label>");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("<div class=\"controls\" id=\"");
    String _elementId_1 = selfield.getElementId();
    _builder.append(_elementId_1, "		");
    _builder.append("\">");
    CharSequence _xblockexpression = null;
    {
      int i = (-1);
      CharSequence _switchResult = null;
      SelectionFieldType _selectionFieldType = selfield.getSelectionFieldType();
      final SelectionFieldType _switchValue = _selectionFieldType;
      boolean _matched = false;
      if (!_matched) {
        if (Objects.equal(_switchValue,SelectionFieldType.CHECKBOX)) {
          _matched=true;
          StringConcatenation _builder_1 = new StringConcatenation();
          _builder_1.newLine();
          {
            EList<SelectionItem> _items = selfield.getItems();
            for(final SelectionItem item : _items) {
              _builder_1.append("\t");
              _builder_1.append("<label class=\"checkbox\" for=\"");
              String _elementId_2 = selfield.getElementId();
              _builder_1.append(_elementId_2, "	");
              _builder_1.append("_");
              int _plus = (i + 1);
              int _i = i = _plus;
              _builder_1.append(_i, "	");
              _builder_1.append("\">");
              _builder_1.newLineIfNotEmpty();
              _builder_1.append("\t");
              _builder_1.append("\t");
              _builder_1.append("<input type=\"checkbox\" value=\"");
              String _label_1 = item.getLabel();
              _builder_1.append(_label_1, "		");
              _builder_1.append("\" name=\"");
              String _elementId_3 = selfield.getElementId();
              _builder_1.append(_elementId_3, "		");
              _builder_1.append("\" id=\"");
              String _elementId_4 = selfield.getElementId();
              _builder_1.append(_elementId_4, "		");
              _builder_1.append("_");
              _builder_1.append(i, "		");
              _builder_1.append("\"");
              {
                boolean _isSelected = item.isSelected();
                if (_isSelected) {
                  _builder_1.append(" checked=\"checked\"");
                }
              }
              _builder_1.append("/> ");
              String _label_2 = item.getLabel();
              _builder_1.append(_label_2, "		");
              _builder_1.newLineIfNotEmpty();
              _builder_1.append("\t");
              _builder_1.append("</label>");
              _builder_1.newLine();
            }
          }
          _switchResult = _builder_1;
        }
      }
      if (!_matched) {
        if (Objects.equal(_switchValue,SelectionFieldType.RADIO)) {
          _matched=true;
          StringConcatenation _builder_2 = new StringConcatenation();
          _builder_2.newLine();
          {
            EList<SelectionItem> _items_1 = selfield.getItems();
            for(final SelectionItem item_1 : _items_1) {
              _builder_2.append("\t");
              _builder_2.append("<label class=\"radio\" for=\"");
              String _elementId_5 = selfield.getElementId();
              _builder_2.append(_elementId_5, "	");
              _builder_2.append("_");
              int _plus_1 = (i + 1);
              int _i_1 = i = _plus_1;
              _builder_2.append(_i_1, "	");
              _builder_2.append("\">\t\t");
              _builder_2.newLineIfNotEmpty();
              _builder_2.append("\t");
              _builder_2.append("\t");
              _builder_2.append("<input type=\"radio\" value=\"");
              String _label_3 = item_1.getLabel();
              _builder_2.append(_label_3, "		");
              _builder_2.append("\" name=\"");
              String _elementId_6 = selfield.getElementId();
              _builder_2.append(_elementId_6, "		");
              _builder_2.append("\" id=\"");
              String _elementId_7 = selfield.getElementId();
              _builder_2.append(_elementId_7, "		");
              _builder_2.append("_");
              _builder_2.append(i, "		");
              _builder_2.append("\"");
              {
                boolean _isSelected_1 = item_1.isSelected();
                if (_isSelected_1) {
                  _builder_2.append(" checked=\"checked\"");
                }
              }
              _builder_2.append("/> ");
              String _label_4 = item_1.getLabel();
              _builder_2.append(_label_4, "		");
              _builder_2.newLineIfNotEmpty();
              _builder_2.append("\t");
              _builder_2.append("</label>\t\t\t\t\t");
              _builder_2.newLine();
            }
          }
          _switchResult = _builder_2;
        }
      }
      if (!_matched) {
        if (Objects.equal(_switchValue,SelectionFieldType.COMBOBOX)) {
          _matched=true;
          StringConcatenation _builder_3 = new StringConcatenation();
          _builder_3.newLine();
          _builder_3.append("\t");
          _builder_3.append("<select id=\"");
          String _elementId_8 = selfield.getElementId();
          _builder_3.append(_elementId_8, "	");
          _builder_3.append("\">");
          _builder_3.newLineIfNotEmpty();
          {
            EList<SelectionItem> _items_2 = selfield.getItems();
            for(final SelectionItem item_2 : _items_2) {
              _builder_3.append("\t\t");
              _builder_3.append("<option value=\"");
              String _label_5 = item_2.getLabel();
              _builder_3.append(_label_5, "		");
              _builder_3.append("\"");
              {
                boolean _isSelected_2 = item_2.isSelected();
                if (_isSelected_2) {
                  _builder_3.append(" selected=\"selected\"");
                }
              }
              _builder_3.append(">");
              String _label_6 = item_2.getLabel();
              _builder_3.append(_label_6, "		");
              _builder_3.append("</option>");
              _builder_3.newLineIfNotEmpty();
            }
          }
          _builder_3.append("\t");
          _builder_3.append("</select>");
          _builder_3.newLine();
          _switchResult = _builder_3;
        }
      }
      _xblockexpression = (_switchResult);
    }
    _builder.append(_xblockexpression, "		");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("</div>");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("</label>");
    _builder.newLine();
    _builder.append("</div>\t");
    _builder.newLine();
    return _builder;
  }
  
  public String operator_add(final String string, final String string2) {
    final String out = string.concat(string2);
    return out;
  }
  
  public CharSequence extractPeInfo(final PageElement heading) {
    if (heading instanceof Heading) {
      return _extractPeInfo((Heading)heading);
    } else if (heading instanceof Paragraph) {
      return _extractPeInfo((Paragraph)heading);
    } else if (heading instanceof SelectionField) {
      return _extractPeInfo((SelectionField)heading);
    } else if (heading instanceof TextArea) {
      return _extractPeInfo((TextArea)heading);
    } else if (heading instanceof TextField) {
      return _extractPeInfo((TextField)heading);
    } else if (heading instanceof List) {
      return _extractPeInfo((List)heading);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(heading).toString());
    }
  }
}
